package com.grayson.wificontrol;

import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.widget.RemoteViews;

public class WificontrolWidget extends AppWidgetProvider {
	public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
	
	public static class WificontrolServer extends Service {
		private static boolean mIsRunning = false;
		private RemoteViews mView = null;
		
		public class WifiObserver extends ContentObserver {
			public WifiObserver(Handler handle) {
				super(handle);
			}
			public void onChange(boolean selfChange) {
				Log.d("WifiObserver", "onChange");
				update();
			}
		}
		
		public IBinder onBind(Intent intent) {
			return null;
		}
		
		public void onCreate() {
			Log.d("UpdateService", "onCreate");
			mIsRunning = true;
			mView = new RemoteViews(getPackageName(), R.layout.widget);
		}
		
		public void onDestroy() {
			mIsRunning = false;
		}
		
		public void onStart(Intent intent, int startId) {
			Log.d("UpdateService", "onStart");
			WifiObserver observer = new WifiObserver(new Handler());
			getContentResolver().registerContentObserver(
				android.provider.Settings.System.CONTENT_URI, true, observer);
			update();
		}
		
		private void update() {
			Log.d("UpdateService", "update");
			//mView.setTextViewText(R.id.output_field, "counter=" + ++counter);
			AppWidgetManager.getInstance(this).updateAppWidget(
				new ComponentName(this, WificontrolWidget.class), mView);
		}
		
		public static boolean isRunning() {
			return mIsRunning;
		}
	}
	
	public void onEnabled(Context context)
	{
		super.onEnabled(context);
	}
	
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
	    final int N = appWidgetIds.length;
	    // Perform this loop procedure for each App Widget that belongs to this provider
	    for (int i = 0; i < N; i++) {
	      int appWidgetId = appWidgetIds[i];
	      // Create an Intent to launch ExampleActivity
	      Intent intent = new Intent(context, WificontrolWidget.class);
	      intent.setAction(ACTION_WIDGET_RECEIVER);
	      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
	      
	      // Get the layout for the App Widget and attach an on-click listener
	      // to the button
	      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
	      views.setOnClickPendingIntent(R.id.imageView1, pendingIntent);
	      
	      // Update the widget image according to current WiFi sleep policy
	      setWidgetImage(context);
	      
	      // Tell the AppWidgetManager to perform an update on the current app
	      // widget
	      appWidgetManager.updateAppWidget(appWidgetId, views);
	    }
	    super.onUpdate(context, appWidgetManager, appWidgetIds);
	}
	
    public void onReceive(Context context, Intent intent) 
    {
    	if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) 
        {
    		wifiPolicyToggle.togglePolicy(context);
    		setWidgetImage(context);
        }
    	else
    	{
    		//Toast.makeText(context, intent.getAction() , Toast.LENGTH_SHORT).show();
    		setWidgetImage(context);
    	}

    	super.onReceive(context, intent);
    }
    
    public void setWidgetImage(Context context)
    {
    	RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
		
		switch(wifiPolicyToggle.getCurrentPolicy(context))
		{
		case Settings.System.WIFI_SLEEP_POLICY_DEFAULT:
			views.setImageViewResource(R.id.imageView1, R.drawable.wifi_sleep);
    		break;
    	case Settings.System.WIFI_SLEEP_POLICY_NEVER:
			views.setImageViewResource(R.id.imageView1, R.drawable.wifi_always);
    		break;
    	case Settings.System.WIFI_SLEEP_POLICY_NEVER_WHILE_PLUGGED:
			views.setImageViewResource(R.id.imageView1, R.drawable.wifi_plugged);
    		break;
    	default:
    		
    		break;
		}
		int[] ids;
		ComponentName thisWidget = new ComponentName(context, WificontrolWidget.class);
		ids = AppWidgetManager.getInstance(context).getAppWidgetIds(thisWidget);
		for(int i=0; i<ids.length; i++)
		{
			AppWidgetManager.getInstance(context).updateAppWidget(ids[i], views);
		}
    }
}
